package dao

import (
	"haiqiu-go-admin/entity"
	"haiqiu-go-admin/entity/dto"
	"haiqiu-go-admin/utils"
	"log"
	"strconv"
)

type LoginLogDao struct {
}

// 分页查询总条数
func (LoginLogDao) LoginLogPageCount(page dto.LoginLogQueryDto) (count int64) {
	wrapper := utils.QueryWrapper().Count("sys_login_log")
	getLoginLogCondition(&page, wrapper)
	err := DB.QueryRow(wrapper.GetSql(), wrapper.GetVal()...).Scan(&count)
	if err != nil {
		log.Println(err.Error())
	}
	return count
}

// 分页查询数据
func (LoginLogDao) LoginLogPageList(page dto.LoginLogQueryDto) (*[]entity.SysLoginLog, error) {
	wrapper := utils.QueryWrapper().Page("sys_login_log", page.PageIndex, page.PageSize).
		OrderByDesc("create_time")
	getLoginLogCondition(&page, wrapper)
	rows, err := DB.Query(wrapper.GetSql(), wrapper.GetVal()...)
	if err != nil {
		log.Println(err.Error())
		return nil, err
	}
	var sysLoginLogs []entity.SysLoginLog
	for rows.Next() {
		var sysLoginLog = entity.SysLoginLog{}
		err := rows.Scan(
			&sysLoginLog.Id,
			&sysLoginLog.RequestParam,
			&sysLoginLog.ResponseJson,
			&sysLoginLog.RequestIp,
			&sysLoginLog.RequestAddress,
			&sysLoginLog.Username,
			&sysLoginLog.RestUrl,
			&sysLoginLog.Result,
			&sysLoginLog.Os,
			&sysLoginLog.Browser,
			&sysLoginLog.CreateTime,
			&sysLoginLog.UpdateTime,
		)
		if err != nil {
			log.Println(err.Error())
			return nil, err
		}
		sysLoginLogs = append(sysLoginLogs, sysLoginLog)
	}
	return &sysLoginLogs, nil
}

func getLoginLogCondition(page *dto.LoginLogQueryDto, wrapper *utils.Wrapper) {
	if page.Params.Username != nil {
		wrapper.Eq("username", *page.Params.Username)
	}
	if page.Params.RequestAddress != nil {
		wrapper.Like("request_address", *page.Params.RequestAddress)
	}
	if page.Params.Result != nil {
		wrapper.Eq("result", *page.Params.Result)
	}
	if page.Params.BeginTime != nil && page.Params.EndTime != nil {
		wrapper.Between("create_time", *page.Params.BeginTime,
			*page.Params.EndTime)
	}
}

func (LoginLogDao) LoginLogById(id int64) (*entity.SysLoginLog, error) {
	wrapper := utils.QueryWrapper().GetById("sys_login_log", id)
	stmt, err := DB.Prepare(wrapper.GetSql())
	if err != nil {
		panic(err)
	}
	row := stmt.QueryRow(wrapper.GetVal())
	var sysLoginLog entity.SysLoginLog
	err = row.Scan(
		&sysLoginLog.Id,
		&sysLoginLog.RequestParam,
		&sysLoginLog.ResponseJson,
		&sysLoginLog.RequestIp,
		&sysLoginLog.RequestAddress,
		&sysLoginLog.Username,
		&sysLoginLog.RestUrl,
		&sysLoginLog.Result,
		&sysLoginLog.Os,
		&sysLoginLog.Browser,
		&sysLoginLog.CreateTime,
		&sysLoginLog.UpdateTime,
	)
	if err != nil {
		panic(err)
	}
	return &sysLoginLog, nil
}

// LoginLogAdd 新增数据
func (LoginLogDao) LoginLogAdd(sysLoginLog *entity.SysLoginLog) (int64, error) {
	exec, err := DB.Exec("insert into `sys_login_log` (`id`, `request_param`, `response_json`, "+
		"`request_ip`, `request_address`, `username`, `rest_url`, `result`, `os`, `browser`, "+
		"`create_time`, `update_time` ) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )",
		&sysLoginLog.Id,
		&sysLoginLog.RequestParam,
		&sysLoginLog.ResponseJson,
		&sysLoginLog.RequestIp,
		&sysLoginLog.RequestAddress,
		&sysLoginLog.Username,
		&sysLoginLog.RestUrl,
		&sysLoginLog.Result,
		&sysLoginLog.Os,
		&sysLoginLog.Browser,
		&sysLoginLog.CreateTime,
		&sysLoginLog.UpdateTime,
	)
	if err != nil {
		return 0, err
	}
	rowsAffected, err := exec.RowsAffected()
	if err != nil {
		return 0, err
	}
	return rowsAffected, nil
}

// LoginLogUpdate 根据ID更新数据
func (LoginLogDao) LoginLogUpdate(sysLoginLog *entity.SysLoginLog) (int64, error) {
	exec, err := DB.Exec("update `sys_login_log` set `request_param` = ?, "+
		"`response_json` = ?, `requestIp` = ?, `request_address` = ?, `username` = ?, "+
		"`restUrl` = ?, `result` = ?, `os` = ?, `browser` = ?, `create_time` = ?, "+
		"`update_time` = ? where `id` = ?",
		&sysLoginLog.RequestParam,
		&sysLoginLog.ResponseJson,
		&sysLoginLog.RequestIp,
		&sysLoginLog.RequestAddress,
		&sysLoginLog.Username,
		&sysLoginLog.RestUrl,
		&sysLoginLog.Result,
		&sysLoginLog.Os,
		&sysLoginLog.Browser,
		&sysLoginLog.CreateTime,
		&sysLoginLog.UpdateTime,
		&sysLoginLog.Id,
	)
	if err != nil {
		return 0, err
	}
	rowsAffected, err := exec.RowsAffected()
	if err != nil {
		return 0, err
	}
	return rowsAffected, nil
}

// LoginLogDelete 根据ID删除数据
func (LoginLogDao) LoginLogDelete(id int64) (int64, error) {
	exec, err := DB.Exec("delete from sys_login_log where id = ?")
	if err != nil {
		panic(err)
		return 0, err
	}
	rowsAffected, err := exec.RowsAffected()
	if err != nil {
		panic(err)
		return 0, err
	}
	return rowsAffected, nil
}

// LoginLogDeleteBatch 根据ID批量删除数据
func (LoginLogDao) LoginLogDeleteBatch(ids []int64) (int64, error) {
	var idsStr string
	for index, id := range ids {
		if index == len(ids)-1 {
			idsStr += strconv.FormatInt(id, 10)
		} else {
			idsStr += strconv.FormatInt(id, 10) + ", "
		}
	}
	exec, err := DB.Exec("delete from sys_dept where id in (?)", idsStr)
	if err != nil {
		panic(err)
		return 0, err
	}
	rowsAffected, err := exec.RowsAffected()
	if err != nil {
		panic(err)
		return 0, err
	}
	return rowsAffected, nil
}
